// pages/mine/mine.js
const app = getApp()
import Notify from '@vant/weapp/notify/notify';
Page({

  data: {
    userInfo: {},
		count:{},
		type:[],
    likes:[],
    loadModal:true
  },
  onShow() {
    this.getTabBar().init();
    //页面加载时从userInfo中获取用户信息，如果没有就弹出用户授权页这个判断要同步到发布动态（tab切换到生活面板就应该做判断）、查看最近消息（tab切换到消息）评论文章、回复评论。这几个操作中也要用户完成初始化才能执行后续操作。直接跳转到个人中心，程序自动完成初始化工作，再由用户自行修改信息
    this.setData({
			userInfo: wx.getStorageSync('user'),
			likes:wx.getStorageSync('likes')
		})
		this.getAllKind()
    var info = this.data.userInfo
    if (info == null || info == '') {
      console.log('未登录')
      //调用接口获取用户基本信息，并保存在storage中去
      app.request({
        url: app.globalData.apiUrl + "/user/get",
        method: 'GET',
      }).then(res => {
        if (res.statusCode == 200) {
          wx.setStorageSync('user', res.data.data)
          this.setData({
            userInfo: wx.getStorageSync('user')
          })
        } else {
          Notify({
						type: 'primary',
						message: '服务错误'
					})
        }
      })
    }
    app.request({
      url: app.globalData.apiUrl + "/follow",
      method: 'GET',
    }).then(res => {
      if (res.statusCode == 200) {
        this.setData({
          count:res.data.data,
          loadModal:false
        })
      } else {
        this.setData({
          loadModal:false
        })
        Notify('服务错误，稍后重试');
      }
    })
  },
  //页面跳转函数
  gotoattention() {
    wx.navigateTo({
      url: '/pages/mine/attention/attention',
    })
  },
  gotofollow() {
    wx.navigateTo({
      url: '/pages/mine/follow/follow',
    })
	},
	gotoShow(){
		wx.navigateTo({
			url: '/pages/show/show?id='+this.data.userInfo.id,
		})
	},
	showModal(e) {
		this.setData({
			modalName: e.currentTarget.dataset.target,
		})
	},
	hideModal(e) {
		this.setData({
			modalName: null,
		})
		console.log(this.data)
	},
	getAllKind() {
    let kind = wx.getStorageSync('kind')
    if (kind == null || kind == '') {
      app.request({
        url: app.globalData.apiUrl + "/passage/kind",
        method: 'GET',
      }).then(res => {
        if (res.statusCode == 200) {
          wx.setStorageSync('kind', res.data.data)
          this.setData({
            type: res.data.data
          })
        } else {
          wx.showToast({
            title: "服务错误",
            icon: 'error',
            duration: 1000
          })
        }
      })
    } else {
      this.setData({
        type: kind
      })
    }
	},
	onChange(event) {
    this.setData({
      likes: event.detail,
		});
		wx.setStorageSync('likes', event.detail)
  },
})